package com.tfxy.hxl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tfxy.hxl.entity.Course;
import com.tfxy.hxl.entity.vo.CourseFrontVo;
import com.tfxy.hxl.entity.vo.CourseQuery;
import com.tfxy.hxl.mapper.CourseMapper;
import com.tfxy.hxl.service.CourseService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 课程服务实现类
 * 继承ServiceImpl，实现CourseService接口
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {

    @Override
    public void pageQuery(Page<Course> pageParam, CourseQuery courseQuery) {
        // 构建查询条件
        QueryWrapper<Course> wrapper = new QueryWrapper<>();
        wrapper.eq("deleted", 0);
        
        if (courseQuery != null) {
            if (StringUtils.hasText(courseQuery.getTitle())) {
                wrapper.like("title", courseQuery.getTitle());
            }
            if (StringUtils.hasText(courseQuery.getInstructor())) {
                wrapper.like("instructor", courseQuery.getInstructor());
            }
            if (courseQuery.getCategoryId() != null) {
                wrapper.eq("category_id", courseQuery.getCategoryId());
            }
            if (courseQuery.getDifficultyLevel() != null) {
                wrapper.eq("difficulty_level", courseQuery.getDifficultyLevel());
            }
            if (courseQuery.getStatus() != null) {
                wrapper.eq("status", courseQuery.getStatus());
            }
            if (StringUtils.hasText(courseQuery.getBeginTime())) {
                wrapper.ge("create_time", courseQuery.getBeginTime());
            }
            if (StringUtils.hasText(courseQuery.getEndTime())) {
                wrapper.le("create_time", courseQuery.getEndTime());
            }
        }
        
        wrapper.orderByDesc("create_time");
        
        // 执行查询
        baseMapper.selectPage(pageParam, wrapper);
    }

    @Override
    public Map<String, Object> getCourseFrontInfo(Page<Course> pageCourse, CourseFrontVo courseFrontVo) {
        // 构建查询条件
        QueryWrapper<Course> wrapper = new QueryWrapper<>();
        wrapper.eq("status", 1).eq("deleted", 0);
        
        if (courseFrontVo.getCategoryId() != null) {
            wrapper.eq("category_id", courseFrontVo.getCategoryId());
        }
        if (courseFrontVo.getDifficultyLevel() != null) {
            wrapper.eq("difficulty_level", courseFrontVo.getDifficultyLevel());
        }
        if (StringUtils.hasText(courseFrontVo.getKeyword())) {
            wrapper.like("title", courseFrontVo.getKeyword());
        }
        
        // 排序处理
        if (StringUtils.hasText(courseFrontVo.getBuyCountSort())) {
            wrapper.orderByDesc("enroll_count");
        }
        if (StringUtils.hasText(courseFrontVo.getPriceSort())) {
            wrapper.orderByDesc("price");
        }
        if (StringUtils.hasText(courseFrontVo.getGmtCreateSort())) {
            wrapper.orderByDesc("create_time");
        }
        if (StringUtils.hasText(courseFrontVo.getRatingSort())) {
            wrapper.orderByDesc("rating");
        }
        
        baseMapper.selectPage(pageCourse, wrapper);
        
        // 构建返回数据
        Map<String, Object> map = new HashMap<>();
        map.put("total", pageCourse.getTotal());
        map.put("list", pageCourse.getRecords());
        map.put("size", pageCourse.getSize());
        map.put("current", pageCourse.getCurrent());
        map.put("pages", pageCourse.getPages());
        map.put("hasPrevious", pageCourse.hasPrevious());
        map.put("hasNext", pageCourse.hasNext());
        
        return map;
    }

    @Override
    public boolean publishCourse(Long courseId) {
        if (courseId == null) {
            return false;
        }
        
        UpdateWrapper<Course> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", courseId)
                    .set("status", 1)
                    .set("update_time", new Date());
        
        return update(updateWrapper);
    }

    @Override
    public Course getCourseDetail(Long courseId) {
        if (courseId == null) {
            return null;
        }
        
        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", courseId).eq("deleted", 0);
        
        Course course = getOne(queryWrapper);
        
        // 增加浏览次数（异步处理，不影响查询性能）
        if (course != null) {
            UpdateWrapper<Course> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("id", courseId)
                        .setSql("view_count = view_count + 1");
            update(updateWrapper);
        }
        
        return course;
    }
}
